<template>
  <div class="add-dept">
    <el-dialog :visible="showdialog" :title="type==='add'?'新增部门':'编辑部门'" @close="cancel">
      {{ type }}
      <el-form ref="myform" :model="form" :rules="rules">
        <el-form-item label="部门名称" label-width="100px" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="部门编码" label-width="100px" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="部门负责人" label-width="100px" prop="manager">
          <el-select v-model="form.manager" placeholder="请选择">
            <el-option
              v-for="(item,index) in userList"
              :key="index"

              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" label-width="100px" prop="introduce">
          <el-input v-model="form.introduce" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { companyDepartmentApi, addDeapartApi, changeDepartment } from '@/api/departments'
import { usersimpleApi } from '@/api/employees'
export default {
  props: ['showdialog', 'currentitem', 'type'],
  data() {
    return {
      userList: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      value: '',
      rules: {
        // 部门名称校验
        name: [{
          required: true, message: '部门名称不能为空', trigger: 'blur'
        }, { min: 1, max: 50, message: '部门名称1-50个字符', trigger: 'blur' }, {
          validator: async(rules, value, callback) => {
            const res = await companyDepartmentApi()
            var bool
            // 新增面板校验规则
            if (this.type === 'add') {
              bool = res.depts.filter(item => {
                return item.pid === this.currentitem.id
              }).some(item => {
                return item.name === value
              })
            } else if (this.type === 'view') {
              // 编辑面板校验规则
              bool = res.depts.filter(item => {
                return item.pid === this.currentitem.pid
              }).some(item => {
                return item.name === value && item.id !== this.currentitem.id
              })
            }
            bool ? callback(new Error('部门名称不能重复')) : callback()
          }
        }],
        // 部门编码校验
        code: [{
          required: true, message: '部门编码不能为空', trigger: 'blur'
        }, { min: 1, max: 50, message: '部门编码1-50个字符', trigger: 'blur' },
        {
          validator: async(rules, value, callback) => {
            const res = await companyDepartmentApi()
            var bool
            if (this.type === 'add') {
              bool = res.depts.some(item => {
                return item.code === value
              })
            } else {
              bool = res.depts.some(item => {
                return item.code === value && item.id !== this.currentitem.id
              })
            }
            bool ? callback(new Error('部门编码不能重复')) : callback()
          }
        }],
        // 部门负责人校验
        manager: [{
          required: true, message: '部门负责人不能为空', trigger: 'blur'
        }],
        // 部门介绍
        introduce: [{
          required: true, message: '部门介绍不能为孔', trigger: 'blur'
        }, { min: 1, max: 300, message: '部门介绍1-300个字符', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 得到所有的负责人列表
    this.getUserSimple()
  },
  methods: {
    // 获取部门详情
    async getDepartDetail(id) {
      this.formData = await changeDepartment(id)
    },
    // 得到所有的负责人列表
    async getUserSimple() {
      const res = await usersimpleApi()
      this.userList = res
    },
    cancel() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
      this.$refs.myform.resetFields()
      this.$emit('closepanel', false)
    },
    submit() {
      this.$refs.myform.validate(async isOK => {
        if (isOK) {
          try {
            if (this.type === 'add') {
              await addDeapartApi({
                ...this.form,
                pid: this.currentitem.pid === '-1' ? '' : this.currentitem.id })
            } else {
              await changeDepartment(
                this.form
              )
            }

            // 关闭面板
            this.$emit('closepanel')
            this.form = {
              name: '',
              code: '',
              manager: '',
              introduce: '',
              pid: ''
            }
            // 提示信息
            this.$message.success('操作成功')
            this.$emit('updatedata')
          } catch (err) {
            console.log('提交错误', err)
          }
        }
      })
    }
  }
}
</script>

<style>

</style>
